package com.power.algorithm;

/**
 * @description:
 * @author: jiangnan
 * @time: 2021/6/18 4:37 下午
 */
public class Solution8 {
    public int myAtoi(String s) {
        char[] ch = s.trim().toLowerCase().toCharArray();
        StringBuilder stringBuilder = new StringBuilder();


        // 判断开始以+— 开始的
        //判断以数字开始的
        //超出Integer的


        for (int j = 0; j < ch.length; j++) {
            if (j == 0 && ch[j] == '-' || ch[j] == '+') {
                stringBuilder.append(ch[j]);
            } else if (Character.isDigit(ch[j])) {
                stringBuilder.append(ch[j]);
            } else if (ch[j] == ' ') {
                continue;
            } else if (!Character.isDigit(ch[j])) {
                break;
            }
        }
        String str = stringBuilder.toString();
        //判断空串
        if (str.isEmpty()) {
            return 0;
        }

        long l = Long.parseLong(str);
        if (l > (long) Integer.MAX_VALUE) {
            return Integer.MAX_VALUE;
        } else if (l < (long) Integer.MIN_VALUE) {
            return Integer.MIN_VALUE;
        }

        return Integer.parseInt(str);
    }

    public static void main(String[] args) {
        String s1 = "words and 987";
        String s = "-4193 with words";
        String s2 = "-91283472332";
        String s3 = "42";
        String s4 = "3.14159";
        Solution8 solution8 = new Solution8();
/*        System.out.println(solution8.myAtoi(s1));
        System.out.println(solution8.myAtoi(s2));

        System.out.println(solution8.myAtoi(s3));*/
        System.out.println(solution8.myAtoi(s4));
    }
}
